<template>
	<om-addpage style="padding-top: 35px;" :busCode="busCode" :menuName="menuName" @formSubmit="formSubmit">
		<om-autoform ref="autoform" :initConfigData="mainConfigData" :initValue="initValue"></om-autoform>
	</om-addpage>
</template>

<script>
	import {
		errorModal,
		ignoreHtml,
		getUrl,
		confirmModal,
		ptFloat,
		minusF
	} from '@/static/js/common.js';
	import {
		changeConfigAttr,
		buildFormData,
		formValidation,
		tranferToEditors,
		changeEditor,
		setSubmitSubValue,
		configSelectData,
		hasValue,
		hasEditor,
		formShowLoading,
		getFormFields,
		formModelLoad,
		getWorkCacheKey,
		getEditorTitle
		
	} from '@/static/js/formUtil.js';
	import urlUtil from '@/static/js/urlUtil.js';
	import { mapState } from 'pinia';
	import {useLoginUserStore} from '@/stores/login-user.js';
	import moment from '@/static/js/moment.js'
	export default {
		provide() {
			return {
				formPage: this
			};
		},
		data() {
			return buildFormData({
				codeNo: '',
				findCustomerByPosUserCacheKey: 'mar-yxhd-customerComplaint-findCustomerByPosUser-cache',
				productShowCacheKey: 'mar-yxhd-customerComplaint-productShow-cache',
				customerClassIdCacheKey: 'mar-yxhd-customerComplaint-customerClassId-cache',
				marketingProductCategoryCache:'mar-yxhd-customerComplaint-marketingProductCategory-cache'
			})
		},
		computed: {
			...mapState(useLoginUserStore, ['userName', 'userId','salaryPosId','salaryPosName','salaryInstId','salaryInstName'])
		},
		methods:{
			onSelectLaunchDate(val){
				let _that = this
				let formdata = _that.$refs.autoform.formdata
				if(formdata.problemDiscoveryDate){
					if(moment(val).isBefore(formdata.problemDiscoveryDate)){
						let title1 = _that.$refs.autoform.getEditorTitle("title2","launchDate")
						let title2 = _that.$refs.autoform.getEditorTitle("title2","problemDiscoveryDate")
						errorModal(_that,'提示信息',title1+'不能早于'+title2+formdata.problemDiscoveryDate,()=>{
							_that.$refs.autoform.clear("title2","launchDate")
						})
					}
				}
			},
			onSelectDiscoveryDate(val){
				let _that = this
				let formdata = _that.$refs.autoform.formdata
				if(formdata.launchDate){
					if(moment(val).isAfter(formdata.launchDate)){
						let title1 = _that.$refs.autoform.getEditorTitle("title2","launchDate")
						let title2 = _that.$refs.autoform.getEditorTitle("title2","problemDiscoveryDate")
						errorModal(_that,'提示信息',title2+'不能晚于'+formdata.launchDate,()=>{
							_that.$refs.autoform.clear("title2","problemDiscoveryDate")
						})
					}
				}
			},
			clearCustomer(){
				let _that = this
				_that.$refs.autoform.clear("title2","customerId")
				_that.$refs.autoform.clear("title2","customerCode")
				_that.$refs.autoform.clear("title2","customerShortName")
				_that.$refs.autoform.clear("title2","customerName")
				_that.$refs.autoform.clear("title2","businessLeaderId")
				_that.$refs.autoform.clear("title2","mainOwnerId")
			},
			formSubmit(){
				let _that = this
				if(_that.formdata.dataStr){
					let dataList = JSON.parse(_that.formdata.dataStr)
					for(let index in dataList){
						if(!dataList[index].fileList){
							delete dataList[index].fileList
						}
					}
					_that.formdata.dataStr = JSON.stringify(dataList)
				}
				if(_that.edit){
					_that.om.formSave("customerComplaint/doUpdate",_that.formdata,_that)
				}else{
					_that.om.formSave("customerComplaint/doAdd",_that.formdata,_that)
				}
			},
			initLoad(options){
				let _that = this
				let fileId = options.fileId
				_that.codeNo = options.code
				let ajaxArr = [
					urlUtil.zposUrl(options.code),
					{
						url:"orgInstPosUserOfficial/getPosComboboxByUser",
						param:{
							'userId':_that.userId
						}
					},
					{
						url:"tsysdict/getCombobox",
						param:{
							code:'39607'
						}
					}]
					if(fileId){
						ajaxArr.push({
							url:"customerComplaint/getValue",
							param:{
								fileId:fileId
							}
						})
					}

				formModelLoad(ajaxArr,function(res,mainData){
					changeEditor("topinfo","zposId","data",configSelectData(res[0],'id','posname'),mainData)
					changeEditor("title2","postId",{
						defaultName:_that.salaryPosName,
						defaultValue:_that.salaryPosId
					},null,mainData)
					changeEditor("title2","instId",{
						defaultName:_that.salaryInstName,
						defaultValue:_that.salaryInstId
					},null,mainData)
					changeEditor("topinfo","zinstId",{
						defaultName:_that.salaryInstName,
						defaultValue:_that.salaryInstId
					},null,mainData)
					changeEditor("title2","complaintCategoryId","data",configSelectData(res[2],'id','dicname'),mainData)
					changeEditor("title2","marketingProductCategoryId",{
						link:'../../public/search/search',
						"linkParam":{
							other:JSON.stringify({}),
							url:'inventoryCategory/getExistingProductLineAssortment',
							namefield:'name',
							initload:true,
							cachename:_that.marketingProductCategoryCache
						}
					},null,mainData)
					changeEditor("title2","newProductId",{
						"link":"../../public/pageSearch/pageSearch",
						"linkParam":{
							other:JSON.stringify({
								likeFlag: true,
							}),
							url:"productShow/getFormList",
							cachename:_that.productShowCacheKey,
							initload:true,
							searchfield: 'keywords',
							namefield:'newProductName',
							notefield:'newProductStd',
						}
					},null,mainData)
					changeEditor('title2',"businessLeaderId",{
						link:'../../public/searchUser/searchUser',
						"linkParam":{
							cachename: _that.businessLeaderCacheKey,
							isSingle:true
						}
					},null,mainData)
					changeEditor("title2","customerClassId",{
						"link":"../../public/pageSearch/pageSearch",
						"linkParam":{
							other:JSON.stringify({
								busCode: _that.codeNo,
								posId:'',
								userId: _that.userId
							}),
							url:"customerShow/getCustomerCategory",
							cachename:_that.customerClassIdCacheKey,
							initload:true,
							searchfield: 'q',
							namefield:'name',
							notefield:'code'
						}
					},null,mainData)
					if(_that.subEditorObj["title1"]){
						let attr = {}
						let configData = _that.subConfigData["title1"]
						if (hasEditor('title1', 'newProductName', configData)) {
							let title = getEditorTitle("title1","newProductName",configData)
							attr.titleTemplate = title+':<%= newProductName %>'
						}
						changeEditor("title1","dataStr",attr,mainData)
					}
					if(fileId){
						let fieldArr = getFormFields(mainData)
						fieldArr.push("id")
						for(let index in res[3].value){
							if(fieldArr.includes(index)){
								_that.$refs.autoform.$set(_that.$refs.autoform.formdata,index,res[3].value[index])
								_that.$set(_that.formdata,index,res[3].value[index])
							}
						}
						if(_that.subEditorObj["title1"]){
							let datalist = res[3].value.dataList
							_that.subEditorObj["title1"].workList = datalist
							_that.$refs.autoform.$set(_that.$refs.autoform.formdata,"dataStr",JSON.stringify(datalist))
							_that.$set(_that.formdata,"dataStr",JSON.stringify(datalist))
						}
						_that.$nextTick(function(){
							_that.$refs.autoform.initSelectIndex("title2","complaintCategoryId")
						})
					}
					_that.$nextTick(function(){
						if(!_that.$refs.autoform.formdata.zposId){
							_that.$refs.autoform.initSelectVal("topinfo","zposId")
						}else{
							_that.$refs.autoform.changeEditor("topinfo","zposId","type","labelbox")
						}
						
						_that.$refs.autoform.formdata.complaintUserId = _that.userId
						_that.$refs.autoform.formdata.complaintUserName = _that.userName
						
						
						if(!_that.$refs.autoform.formdata.launchDate){
							_that.$refs.autoform.formdata.launchDate = _that.nowDate()
						}
						
						if(!_that.$refs.autoform.formdata.problemDiscoveryDate){
							_that.$refs.autoform.formdata.problemDiscoveryDate = _that.nowDate()
						}
						
						_that.$refs.autoform.changeEditor("title2","customerId", {
							link: "../../public/pageSearch/pageSearch",
							"linkParam": {
								other: JSON.stringify({
									userId: _that.$refs.autoform.formdata.complaintUserId,
									zposId: _that.$refs.autoform.formdata.postId,
									customerClassId: _that.$refs.autoform.formdata.customerClassId,
									busCode: _that.codeNo,
									oldValue:'',
									zuserId:_that.$refs.autoform.formdata.complaintUserId
								}),
								url:"customerShow/getDataPager",
								initload: true,
								searchfield: 'q',
								cachename: _that.findCustomerByPosUserCacheKey,
								namefield: "customerName",
							}
						})
					})
				},options)
			},
			nowDate(){
				var myDate = new Date();
				var year = myDate.getFullYear();
				var month = (myDate.getMonth()+1) < 10 ? '0'+(myDate.getMonth()+1) : (myDate.getMonth()+1);
				var day = myDate.getDate() < 10 ? '0'+myDate.getDate() : myDate.getDate();
				return year + '-' + month + '-' + day;
			},
			onSelectPost(item){
				let _that = this
				_that.$refs.autoform.formdata.instId = item.instId
				_that.$refs.autoform.formdata.instName = item.instName
				_that.om.request('customerComplaint/customerValidation', {
					userId: _that.userId,
					postId: _that.$refs.autoform.formdata.postId
				}, 'post').then(res => {
					if(!res.succeed){
						_that.om.toast("此岗位无客户数据！")
					}
				})
			},
			clearPost(){
				this.$refs.autoform.clear('title2', 'postId');
				this.$refs.autoform.clear('title2', 'postName');
				this.$refs.autoform.clear('title2', 'instId');
				this.$refs.autoform.clear('title2', 'instName');
			},
			clearCustomerClassName(){
				this.$refs.autoform.clear('title2', 'customerClassId');
				this.$refs.autoform.clear('title2', 'customerClassName');
				this.clearCustomerCode()
			},
			clearCustomerCode(){
				this.$refs.autoform.clear('title2', 'customerId');
				this.$refs.autoform.clear('title2', 'customerCode');
				this.$refs.autoform.clear('title2', 'customerCoding');
				this.$refs.autoform.clear('title2', 'customerShortName');
				this.$refs.autoform.clear('title2', 'customerName');
				this.$refs.autoform.clear('title2', 'businessLeaderId');
				this.$refs.autoform.clear('title2', 'businessLeaderName');
				this.$refs.autoform.clear('title2', 'mainOwnerId');
				this.$refs.autoform.clear('title2', 'mainOwnerName');
			}
		},
		onLoad(options) {
			let _that = this
			_that.initLoad(options)
		},
		onShow() {
			let _that = this
			if(_that.$refs.autoform){
				for (var fieldId in _that.subEditorObj) {
					var editor = _that.subEditorObj[fieldId];
					uni.getStorage({
						key: getWorkCacheKey(fieldId),
						success: function(res) {
							editor.workList = res.data;
							uni.removeStorage({
								key: getWorkCacheKey(fieldId)
							});
						}
					});
				}
				uni.getStorage({
					key:_that.findCustomerByPosUserCacheKey,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.customerId = d.id
							formdata.customerCode = d.customerCode
							formdata.customerCoding = d.customerId
							formdata.customerShortName = d.customerShortName
							formdata.customerName = d.customerName
							formdata.businessLeaderId = d.customerOwnerId
							formdata.businessLeaderName = d.customerOwnerName
							formdata.mainOwnerId = d.customerOwnerId
							formdata.mainOwnerName = d.customerOwnerName
							uni.removeStorage({
								key:_that.findCustomerByPosUserCacheKey
							})
						}
					}
				})
				uni.getStorage({
					key:_that.productShowCacheKey,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.newProductId = d.id
							formdata.newProductName = d.newProductName
							formdata.newProductStd = d.newProductStd
							formdata.newProductCode = d.newProductCode
							uni.removeStorage({
								key:_that.productShowCacheKey
							})
						}
					}
				})
				uni.getStorage({
					key:_that.marketingProductCategoryCache,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.marketingProductCategoryId = d.id
							formdata.marketingProductCategoryName = d.name
							uni.removeStorage({
								key:_that.marketingProductCategoryCache
							})
						}
					}
				})
				uni.getStorage({
					key:_that.customerClassIdCacheKey,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.customerClassId = d.id
							formdata.customerClassName = d.name
							_that.$refs.autoform.changeEditor("title2","customerId", {
								link: "../../public/pageSearch/pageSearch",
								"linkParam": {
									other: JSON.stringify({
										userId: _that.$refs.autoform.formdata.complaintUserId,
										zuserId:_that.$refs.autoform.formdata.complaintUserId,
										zposId: _that.$refs.autoform.formdata.postId,
										customerClassId: _that.$refs.autoform.formdata.customerClassId,
										busCode: _that.codeNo,
										oldValue:''
									}),
									url:"customerShow/getDataPager",
									initload: true,
									searchfield: 'q',
									cachename: _that.findCustomerByPosUserCacheKey,
									namefield: "customerName",
								}
							})
							uni.removeStorage({
								key:_that.customerClassIdCacheKey
							})
						}
					}
				})
			}
		}
	}
</script>
<style></style>                                                                                                                                                                                                                                                                                                                  
